package com.easytopit.demo.spring.mvc.httpclient;

import java.io.IOException;

import com.alibaba.fastjson.JSON;
import com.easytopit.demo.spring.mvc.domain.JSONResult;
import com.easytopit.demo.spring.mvc.domain.User;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.BufferedSink;

public class Snippet {
	public static void main(String[] args) throws IOException {
		// OkHttpClient client = new OkHttpClient();
		// Request request = new Request.Builder()
		// .url("http://127.0.0.1:8080/usr/get")
		// .addHeader("Content-Type", "application/json")
		// .addHeader("Accept", "application/json").build();
		//// Request request = new Request.Builder()
		//// .url("http://127.0.0.1:8080/usr/get")
		//// .addHeader("Content-Type", "application/xml")
		//// .addHeader("Accept", "application/xml").build();
		// Response response = client.newCall(request).execute();
		// System.out.println(response);
		//
		// String body = response.body().string();
		// System.out.println(body);
		//
		// User user = JSON.parseObject(body, User.class);
		// System.out.println("ye: 解析成功了 -- ");
		// System.out.println(user);

		OkHttpClient client = new OkHttpClient();

//		MediaType jsonType = MediaType.parse("application/json; charset=utf-8");
//		RequestBody requestBody = RequestBody.create(jsonType, "{\"msg\":{\"id\":1,\"name\":\"ok\"},\"status\":1}");
//		Request request = new Request.Builder().url("http://127.0.0.1:8080/usr/add")
//				.addHeader("Content-Type", "application/json").addHeader("Accept", "application/json").post(requestBody)
//				.build();

		// Request request = new
		// Request.Builder().url("http://127.0.0.1:8080/usr/add")
		// .addHeader("Content-Type", "application/json").addHeader("Accept",
		// "application/json")
		// .post(new RequestBody() {
		//
		// @Override
		// public void writeTo(BufferedSink sink) throws IOException {
		// sink.write("{\"msg\":{\"id\":1,\"name\":\"ok\"},\"status\":1}".getBytes("UTF-8"));
		// sink.flush();
		// }
		//
		// @Override
		// public MediaType contentType() {
		// return MediaType.parse("application/json");
		// }
		// }).build();
		// Request request = new Request.Builder()
		// .url("http://127.0.0.1:8080/usr/get")
		// .addHeader("Content-Type", "application/xml")
		// .addHeader("Accept", "application/xml").build();
		
		
//		Response response = client.newCall(request).execute();
//		System.out.println(response);
//
//		String body = response.body().string();
//		System.out.println(body);
//
//		JSONResult jsonResult = JSON.parseObject(body, JSONResult.class);
//		System.out.println("ye: 解析成功了 -- ");
//		System.out.println(jsonResult);
	}
}
